<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="问题" prop="question">
        <el-input
          v-model="queryParams.question"
          placeholder="请输入问题"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-plus" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="questionList" stripe style="width: 100%">
      <el-table-column prop="question" label="问题" >
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" >
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间"> </el-table-column>
      <el-table-column fixed="right" label="操作" >
        <template slot-scope="scope">
          <el-button @click="editRow(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.prevent="deleteRow(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题" prop="question">
              <el-input v-model="form.question" placeholder="请输入问题" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor v-model="form.answer" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestion, addQuestion, editQuestion, deleteQuestion, getQuestion} from "@/api/system/helpCenter";

export default {
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      loading: true,
      total: 0,
      questionList: [],
      queryParams: {
        current: 1,
        size: 10,
        question: undefined,
      },
      // 表单参数
      form: {},
      title: '',
      open: false,
      // 表单校验
      rules: {
        question: [
          { required: true, message: "问题不能为空", trigger: "blur" }
        ],
        answer: [
          { required: true, message: "回答不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
     getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.questionList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    reset() {
      this.form = {
        id: undefined,
        question: undefined,
        answer: undefined,
      };
      this.resetForm("form");
    },
    deleteRow(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除该问题和答案？').then(function() {
        return deleteQuestion({id:id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    editRow(row) {
      this.reset();
      const id = row.id
      getQuestion({id:id}).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问题";
      });
    },
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题";
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editQuestion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    }
  },
};
</script>

<style>
</style>